/**
 * 
 */
package com.game.jumpdino.logic.visual;

import com.game.jumpdino.R;
import com.game.jumpdino.logic.Playground;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;

/**
 * @author HugoGu
 *
 */
public class BreakBamboo extends Holder {
	private float degree = 0;
	private Bitmap left;
	private Bitmap right;
	private int imageOffsetX = -6;
	
	@Override
	public void onHit(VisualBase visual, Playground ground){
		if (visual.getVelocity().y > 0){
			this.setVy(10);
			this.setG(3.0f);
		}
	}
	
	@Override
	public void drawOn(Canvas c){
		if (this.getVelocity().y > 0){
			int w = left.getWidth();
			int h = left.getHeight();
			Matrix mtx = new Matrix();
			mtx.postRotate(-degree);
			Bitmap rotatedBMP = Bitmap
					.createBitmap(left, 0, 0, w, h, mtx, true);
			BitmapDrawable roleft = new BitmapDrawable(rotatedBMP);
			roleft.setBounds(position.left, position.top, position.left
					+ rotatedBMP.getWidth(), position.top + rotatedBMP.getHeight());
			roleft.draw(c);

			int wR = right.getWidth();
			h = right.getHeight();
			mtx = new Matrix();
			mtx.postRotate(degree);
			rotatedBMP = Bitmap.createBitmap(right, 0, 0, wR, h, mtx, true);
			BitmapDrawable roRight = new BitmapDrawable(rotatedBMP);
			roRight.setBounds(position.left + w + imageOffsetX, position.top, position.left
					+ w + rotatedBMP.getWidth() + imageOffsetX, position.top + rotatedBMP.getHeight());
			roRight.draw(c);

			if (degree < 90) {
				degree += 15;
			}
		} else {
			super.drawOn(c);
		}
	}
	
	@Override
	public void onInitialized(Context context){
		super.onInitialized(context);
		left = BitmapFactory.decodeResource(context.getResources(), R.drawable.hillleft);
		right = BitmapFactory.decodeResource(context.getResources(), R.drawable.hillright);
	}
}
